package org.jeecg.modules.leave.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

import org.jeecg.modules.leave.entity.AccessPresetPersonnelSecurityGroup;
import org.jeecg.modules.leave.entity.AgLeave;
import org.jeecg.modules.leave.entity.AgLeavePO;
import org.jeecg.modules.leave.entity.ResultPO;
import org.jeecg.modules.leave.service.IAgLeaveService;
import org.jeecg.modules.lida.entity.GroupPO;
import org.jeecg.modules.lida.service.LidaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.*;

// 请销假一卡通授权
@Service
@Slf4j
public class LeaveYiktsqService implements JavaDelegate {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LidaService lidaService;

    @Autowired
    private IAgLeaveService agLeaveService;

    @SneakyThrows
    @Override
    public void execute(DelegateExecution delegateExecution) {
        String processInstanceId = delegateExecution.getProcessInstanceId();
        AgLeave agLeave = agLeaveService.getOne(new QueryWrapper<AgLeave>().eq("process_instance_id", processInstanceId), false);
        if(agLeave != null) {
            agLeave.setStatus(1);
            agLeaveService.updateById(agLeave);
        }

        // 暂时关闭卡片相关操作
        if(true)
            return;

        // 调用云平台理达军网接口，用户信息中的卡号换取personCode； 再调用理达的web服务设置门禁权限
        String DLM = agLeave.getApplicant();
        Map<String, Object> map = new HashMap<>();
        map.put("DLM", DLM);
        AgLeavePO agLeavePO = restTemplate.postForObject("http://26.29.23.18:18080/sdata/rest/service/dataapi/rest/507aa702-2d51-47f1-b711-3d483e2d7777",map, AgLeavePO.class);
        String personCode = "";
        ArrayList result = agLeavePO.getResult();
        String string = JSONArray.toJSONString(result.get(0));
        JSONObject jsonObject = (JSONObject)JSON.parse(string);
        ResultPO resultPOS = (ResultPO)net.sf.json.JSONObject.toBean(net.sf.json.JSONObject.fromObject(jsonObject), ResultPO.class);
        personCode = resultPOS.getXYKH();

        try {
            //获取人员信息
            String personID = lidaService.doGetPersonCardInfoByCode(personCode);
            Date leaveStarttime = agLeave.getLeaveStarttime();
            Date leaveEndtime = agLeave.getLeaveEndtime();
            //写表
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startTimeString = sdf.format(leaveStarttime);
            String endTimeString = sdf.format(leaveEndtime);
            boolean yuysq = yuysq(personID, startTimeString, endTimeString);
            if (yuysq==true){
                agLeave.setBeizhu("学号"+DLM+","+"卡号"+personCode+","+"请假开始时间"+startTimeString+","+"请假结束"+endTimeString);
                agLeaveService.updateById(agLeave);
            }else{
                agLeave.setBeizhu("门禁设置失败");
                agLeaveService.updateById(agLeave);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        // 理达ic卡授权
//        List<GroupPO> list = lidaService.doGetAccessSecurityGroups("", "", -1, "");
//        List<GroupPO> list2 = new ArrayList<>();
//
//        list.stream().forEach(e -> {
//            if(e.getSecurityGroupName().contains("主楼"))
//                list2.add(e);
//        });
//
//        lidaService.doOperatePersonSecurityGroupID("", "", agLeave.getApplyer(), "", 1, list2);

    }

    public boolean yuysq(String personnalID,String startTime,String endTime) throws Exception {
        Map<String, String> map = new HashMap();
        map.put("personnalID", personnalID);
        map.put("startTime", startTime);
        map.put("endTime", endTime);

        restTemplate.getForObject("http://localhost:8080/lida/yuysqick?personnalID={personnalID}&startTime={startTime}&endTime={endTime}",String.class,map);
        return true;
    }

    public void selectall(){
        String forObject = restTemplate.getForObject("http://localhost:8080/lida/testlist", String.class);
        System.out.println(forObject);
    }


}
